package pers.ijfxu.testngext.log;

import org.slf4j.Logger;
import org.testng.Reporter;

import pers.ijfxu.testngext.context.RunContext;
import pers.ijfxu.testngext.utils.JsonUtils;

/**
 * 报告日志类
 * 通过环境变量"log2reporter"控制是否打印到报告中
 * 使用方法： RePorterLog log =
 * RePorterLog.getLogger(LoggerFactory.getLogger(this.getClass()));
 *
 * @author ijfxu  
 * @since 2021/5/23 0:00
 */
public class RePorterLog  extends Reporter {

    private Logger logger;

    RePorterLog(Logger logger) {
        this.logger = logger;
    }

    public static RePorterLog getLogger(Logger logger) {
        return new RePorterLog(logger);
    }

    public void info(Object message) {
        String msg = JsonUtils.toJsonStr(message);
        log2report(msg);
        this.logger.info(msg);
    }

    public void debug(Object message) {
        String msg = JsonUtils.toJsonStr(message);
        log2report(msg);
        this.logger.debug(msg);
    }

    public void err(Object message) {
        String msg = JsonUtils.toJsonStr(message);
        log2report(msg);
        this.logger.error(msg);
    }

    /**
     * 只打印到报告中，不在终端或者日志文件中显示
     * 
     * @param msg 打印信息
     * @author ijfxu
     * @since 2021/5/23 0:00
     */
    private void log2report(String msg) {
        if (RunContext.log2reporter) {
            log(msg);
        }
    }
}
